package com.objectcarpentry.kaylee.ui;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.State;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.objectcarpentry.kaylee.AbstractPlaylist;
import com.objectcarpentry.kaylee.IPlayable;
import com.objectcarpentry.kaylee.IPlaylist;
import com.objectcarpentry.kaylee.KayleePlugin;
import com.objectcarpentry.kaylee.Playable;

/**
 * The activator class controls the plug-in life cycle
 */
public class KayleeUIPlugin extends AbstractUIPlugin {

	public static final String ID_PREFIX = "com.objectcarpentry.kaylee.ui";
	
	// The plug-in ID
	public static final String PLUGIN_ID = ID_PREFIX;

	// The shared instance
	private static KayleeUIPlugin plugin;
	
	// The current playlist
	private AbstractPlaylist<?> playlist;
	
	
	private Collection<KayleeUIListener> listeners;
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		playlist = KayleePlugin.getDefault().getLibrary();
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static KayleeUIPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public void hookNavigationHandler(IPlaylist p) {
			ICommandService service = (ICommandService) PlatformUI
					.getWorkbench().getActiveWorkbenchWindow().getService(
							ICommandService.class);
			Command navigateCommand = service
					.getCommand("com.objectcarpentry.kaylee.ui.commands.navigatePlaylist");
			State state = navigateCommand.getState("playlist");
			if (state == null) {
				state = new State();
				navigateCommand.addState("playlist", state);
			}
			state.setValue(p);
	}
	

	public void setPlaylist(AbstractPlaylist<?> pl){
		this.playlist = pl;
		firePlaylistChanged();
	}

	private void firePlaylistChanged() {
		for (KayleeUIListener l : getListeners()){
			l.playlistChanged(playlist);
		}
	}

	private Collection<KayleeUIListener> getListeners() {
		if (listeners == null){
			listeners = new ArrayList<KayleeUIListener>();
		}
		return listeners;
	}
	
	public void addListener(KayleeUIListener l){
		getListeners().add(l);
	}
	
	public void removeListener(KayleeUIListener l){
		getListeners().remove(l);
	}

	public AbstractPlaylist<? extends Playable> getPlaylist() {
		return playlist;
	}

	
}
